﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TaiwuZD
{
    /// <summary>
    /// PickActorCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class PickActorCtrl : UserControl
    {

        public CollectionViewSource TotalActorSource { get; set; } = new CollectionViewSource();
         

        public string SearchWord { get; set; }

        public PickActorCtrl()
        {
            InitializeComponent();

            TotalActorSource.Source = Global.Inst.AllRoles;
            TotalActorSource.Filter += TotalActorSource_Filter;

            Global.Inst.PropertyChanged += Inst_PropertyChanged;
        }

        private void Inst_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Global.AllRoles))
                TotalActorSource.Source = Global.Inst.AllRoles; 
        }

        private void TotalActorSource_Filter(object sender, FilterEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(SearchWord))
            {
                e.Accepted = true;
                return;
            }

            var role = e.Item as Role;

            if (role == null)
                e.Accepted = false;
            else
                e.Accepted = role.Name.Contains(SearchWord);
        }

        private void lv_RoleSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Global.Inst.CurrentRole = (e.Source as ListView).SelectedItem as Role;


            var pop = LogicalTreeHelper.GetParent(this) as Popup;
            if (pop != null)
                pop.IsOpen = false;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TotalActorSource?.View?.Refresh();
        }
    }
}
